// pages/personalDetails/personalDetails.ts
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast';
import { updateUserInfo } from "../../utils/api"
let baseURL = "http://192.168.0.101:3000";
let imageURL = "";
let nick = ""
Page({
  data: {
    userInfo: {},
    nickShow: false,
    nick: "",
    nickErrorMessage: "",
    sexShow: false,
    columns: ['女', '男'],
    sex: 0,
    birthday: "",
    birthdayShow: false,
    currentDate: "",
    minDate: new Date("1950-01-01").getTime(),
    formatter(type, value) {
      if (type === 'year') {
        return `${value}年`;
      }
      if (type === 'month') {
        return `${value}月`;
      }
      return value;
    },
  },
  onLoad() {
    this.getUserInfo();
  },
  getUserInfo() {
    const that = this;
    wx.getStorage({
      key: "userInfo",
      success(res) {
        that.setData({
          userInfo: res.data,
          nick: res.data.userBasicInfo_tb.nick,
          sex: res.data.userBasicInfo_tb.sex,
          birthday: res.data.userBasicInfo_tb.birthday,
          currentDate: new Date(res.data.userBasicInfo_tb.birthday).getTime()
        }, () => {
          nick = that.data.nick
        })
      }
    })
  },
  getTime(ms: any) {
    const dt = new Date(ms);
    const year = dt.getFullYear();
    const mon = dt.getMonth() + 1;
    const day = dt.getDate();
    return `${year}-${mon > 9 ? mon : '0' + mon}-${day > 9 ? day : '0' + day}`
  },
  async updateBirthday(event: any) {
    const time = this.getTime(event['detail'])
    await updateUserInfo({
      birthday: time
    })
    const userInfo: any = this.data.userInfo;
    userInfo['userBasicInfo_tb']['birthday'] = time;
    wx.setStorage({
      key: "userInfo",
      data: userInfo
    })
    this.setData({
      birthday: time,
      birthdayShow: false
    })
    Toast.success("性别修改成功")
  },
  onBirthdayShow() {
    this.setData({
      birthdayShow: true
    })
  },
  onBirthdayClose() {
    this.setData({
      birthdayShow: false
    })
  },
  async updateSex(event: any) {
    const { picker, value, index } = event.detail;
    await updateUserInfo({
      sex: index
    })
    const userInfo: any = this.data.userInfo;
    userInfo['userBasicInfo_tb']['sex'] = index;
    wx.setStorage({
      key: "userInfo",
      data: userInfo
    })
    this.setData({
      sex: index,
      sexShow: false
    })
    Toast.success("性别修改成功")
  },
  onSexShow() {
    this.setData({
      sexShow: true
    })
  },
  onSexClose() {
    this.setData({
      sexShow: false
    })
  },
  async onUpdateNick() {
    if (this.data.nickErrorMessage == "" && nick && this.data.nick != nick) {
      await updateUserInfo({
        nick
      })
      const userInfo: any = this.data.userInfo;
      userInfo['userBasicInfo_tb']['nick'] = nick;
      wx.setStorage({
        key: "userInfo",
        data: userInfo
      })
      this.setData({
        nick,
        nickShow: false
      })
      Toast.success("昵称修改成功")
    }
  },
  onNickInput(e: any) {
    nick = e['detail'];
    if (nick.length == 0) {
      this.setData({
        nickErrorMessage: "昵称不允许为空"
      })
      return
    }
    if (nick.length > 10) {
      this.setData({
        nickErrorMessage: "昵称最高允许10位"
      })
      return
    }
    if (nick == this.data.nick) {
      return;
    }
    this.setData({
      nickErrorMessage: ""
    })
  },
  onNickShow() {
    this.setData({
      nickShow: true
    })
  },
  onNickClose() {
    this.setData({
      nickShow: false
    })
  },
  // 修改头像
  async uploadImage(e: any) {
    const that = this;
    const userInfo: any = this.data.userInfo;
    userInfo['userBasicInfo_tb']['headerImage'] = e['detail']['file']['url'];
    wx.setStorage({
      key: "userInfo",
      data: userInfo
    })
    wx.uploadFile({
      url: `${baseURL}/api/file/uploadImage`,
      filePath: e['detail']['file']['url'],
      name: 'image',
      success: async (res: any) => {
        let data = JSON.parse(res.data);
        imageURL = `${data['data'][0]['thumbnail']}`
        await updateUserInfo({
          headerImage: imageURL
        })
        Toast.success("头像修改成功")
      }
    });
  },

  onPageN(e: any) {
    const info = e.currentTarget.dataset.info;
    if (info == "realNameAuth") {
      if (this.data.userInfo.userState != 2) {
        wx.navigateTo({
          url: "../realNameAuth/realNameAuth"
        })
      } else {
        Toast('您已经实名，请勿再点击');
      }

    }
    console.log(info);
  }
})